package com.jn.okhttp.views;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.jn.okhttp.R;
import com.jn.okhttp.adapter.HotAdapter;
import com.jn.okhttp.contract.IHotSearchContact;
import com.jn.okhttp.entity.HotSearch;
import com.jn.okhttp.presenter.HotSearchPresenter;

import java.util.ArrayList;
import java.util.List;

public class HotSearchFragment extends Fragment implements IHotSearchContact.HotSearchView {
    private View view ;
    private Spinner hSpinner ;
    private ListView hListView ;
    private IHotSearchContact.HotSearchPresenter hPresenter ;
    private List<HotSearch.DataItem> hList ;
    private HotAdapter hotAdapter ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.hotsearch , container ,false);
            init();
        }
        return view ;
    }

    private void init(){
        hSpinner = view.findViewById(R.id.hSpinner);
        hListView = view.findViewById(R.id.h_list);

        hPresenter = new HotSearchPresenter(this , getContext());

        hList = new ArrayList<>() ;
        hotAdapter = new HotAdapter(getContext() , R.layout.hot_item , hList);
        hListView.setAdapter(hotAdapter);

        ArrayAdapter<CharSequence> sAdapter = ArrayAdapter.createFromResource(getContext() ,
                R.array.options_array , android.R.layout.simple_spinner_item);
        sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hSpinner.setAdapter(sAdapter);
        hSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedOption = parent.getItemAtPosition(position).toString();
                String type = getHotSearchType(selectedOption) ;
                hPresenter.getData(type);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private String getHotSearchType(String option){
         String type = null;
         switch (option){
             case "百度" :
                 type = "baidu" ;
                 break;
             case "知乎" :
                 type = "zhihu" ;
                 break;
             case "微信" :
                 type = "weixin" ;
                 break;
             case "哔哩哔哩" :
                 type = "bilibili" ;
                 break;
             case "抖音" :
                 type = "douyin" ;
                 break;
             case "腾讯新闻" :
                 type = "news" ;
                 break;
             case "历史上的今天" :
                 type = "lishi" ;
                 break;
             case "今日头条" :
                 type = "toutiao" ;
                 break;
             case "微博" :
                 type = "weibo" ;
                 break;
         }
         return type ;
    }

    @Override
    public void onHttpLoading() {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void showData(HotSearch data) {
        hList = data.getResult();
        hotAdapter.updateData(hList);
    }
}
